"""
Case Type   : 资源池化
Case Name   : 扩容节点xml配置校验
Create At   : 2024.10
Owner       : chen
Description :
    1.xml不配置AZ值
    2.xml指定无效的AZ值（其他数据类型）
    3.xml指定有效的AZ值
    4.恢复环境
    执行缩容；
    恢复集群状态：cm_ctl switchover -a
Expect      :
    1.合理报错
    2.合理报错
    3.扩容成功
    4.缩容成功；恢复环境成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.sta2_node = Node('Standby2DbUser')
        self.commsh = CommonSH('PrimaryDbUser')
        self.db_host = self.sta2_node.db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = "-----step1: xml不配置AZ值 expect: 合理报错-----"
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        xml_name = os.path.basename(xml_path)
        cmd = f'''cp -r {xml_path} /home/{self.s_user}/'''
        self.log.info(cmd)
        self.pri_node.sh(cmd)
        cmd = f'''sed -i 's/<PARAM name="azName" value="AZ1"\/>/<\!--<PARAM name="azName" value="AZ1"\/>-->/g' /home/{self.s_user}/{xml_name}'''
        self.log.info(cmd)
        self.pri_node.sh(cmd)
        cmd = f'''source {macro.DB_ENV_PATH}; 
            gs_expansion -U {self.s_user} -G {self.s_user} -X \
                /home/{self.s_user}/{xml_name} -h {self.db_host}'''
        self.log.info(cmd)
        res = self.pri_root_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Failed to read [azName] of node', res)

        text = '-----step2: xml指定无效的AZ值（其他数据类型） expect: 合理报错-----'
        self.log.info(text)
        cmd = f'''sed -i 's/<\!--<PARAM name="azName" value="AZ1"\/>-->/<PARAM name="azName" value="AZ1"\/>/g' /home/{self.s_user}/{xml_name}'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        cmd = f'''sed -i 's/AZ1/1.234/g' /home/{self.s_user}/{xml_name}'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        xmlr = self.pri_node.sh(f'cat /home/{self.s_user}/{xml_name}').result()
        self.log.info(xmlr)
        cmd = f'''source {macro.DB_ENV_PATH}; 
            gs_expansion -U {self.s_user} -G {self.s_user} -X \
                /home/{self.s_user}/{xml_name} -h {self.db_host}'''
        self.log.info(cmd)
        res = self.pri_root_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('XML configuration failed', res)
        self.pri_node.sh(f'rm -rf /home/{self.s_user}/{xml_name}')

        text = '-----step3: xml指定有效的AZ值 expect: 扩容成功-----'
        self.log.info(text)
        cmd = f''' gs_expansion -U {self.s_user} \
            -G {self.s_user} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 300
            spawn {cmd}
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*lease repeat for database*:"
            send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)
        res = self.commsh.exec_cm_ctl('query', '-Cv')
        self.log.info(res)
        node_arr = res.splitlines()[-1].split('|')
        for node_mes in node_arr:
            self.assertIn('Normal', node_mes)

    def tearDown(self):
        text = '-----step4: 恢复环境-----'
        self.log.info(text)
        text = '-----step4.1: 执行缩容 expect: 缩容成功-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        res = self.sta2_node.sh(f'source {macro.DB_ENV_PATH}; ' \
            f'rm -rf /home/{self.s_user}/*.xml;' \
            f'gs_uninstall --delete-data').result()
        self.log.info(res)
        text = '-----step4.2: 恢复集群到初始状态 expect: 恢复成功-----'
        self.log.info(text)
        res = self.commsh.exec_cm_ctl('switchover', '-a')
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')